<!DOCTYPE html>












  


<html class="theme-next gemini use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">












<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.4.2" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.4.2">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.4.2">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.4.2">


  <link rel="mask-icon" href="/images/logo.svg?v=6.4.2" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '6.4.2',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta property="og:type" content="website">
<meta property="og:title" content="DBA的葵花宝典">
<meta property="og:url" content="https://gczheng.github.io/index.html">
<meta property="og:site_name" content="DBA的葵花宝典">
<meta property="og:locale" content="zh-CN">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="DBA的葵花宝典">






  <link rel="canonical" href="https://gczheng.github.io/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>DBA的葵花宝典</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">DBA的葵花宝典</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">欲练神功,挥刀自宫,如不自宫,也能成功</p>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home menu-item-active">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-about">
    <a href="/about/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档</a>
  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://gczheng.github.io/2018/10/15/001：介绍和安装/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="郑冠常">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="DBA的葵花宝典">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/10/15/001：介绍和安装/" itemprop="url">
                  001：介绍和安装
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-10-15 12:11:00 / 修改时间：15:23:56" itemprop="dateCreated datePublished" datetime="2018-10-15T12:11:00+08:00">2018-10-15</time>
            

            
              

              
            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="001：介绍和安装"><a href="#001：介绍和安装" class="headerlink" title="001：介绍和安装"></a>001：介绍和安装</h1><p>[TOC]</p>
<h1 id="MySQL介绍和安装"><a href="#MySQL介绍和安装" class="headerlink" title="MySQL介绍和安装"></a>MySQL介绍和安装</h1><h2 id="一-MySQL版本选择"><a href="#一-MySQL版本选择" class="headerlink" title="一. MySQL版本选择"></a>一. MySQL版本选择</h2><ol>
<li><code>MySQL5.6</code> 以后的版本，推荐使用<code>官方版本</code>。</li>
<li>Percona：在5.6版本以后，MySQL将Percon之前优化集成到官方版本中;</li>
<li>MariaDB：无INNODB;且核心代码较老</li>
<li>MySQL在5.6以后不断重构源码，安装包越来越大，功能和性能在持续改进</li>
</ol>
<hr>
<h2 id="二-MySQL官方网站介绍"><a href="#二-MySQL官方网站介绍" class="headerlink" title="二. MySQL官方网站介绍"></a>二. MySQL官方网站介绍</h2><p>官方网站：<a href="http://www.mysql.com" target="_blank" rel="noopener">http://www.mysql.com</a></p>
<h3 id="1-Developer-Zone：-MySQL开发工程师板块"><a href="#1-Developer-Zone：-MySQL开发工程师板块" class="headerlink" title="1. Developer Zone： MySQL开发工程师板块"></a>1. <strong>Developer Zone</strong>： MySQL开发工程师板块</h3><ul>
<li>Articles： Oracle工程师自己的博客</li>
<li>Plant MySQL： 和MySQL相关从业人员的博客</li>
<li>Bugs：MySQL BugList</li>
<li>Worklog：开发记录</li>
<li>Labs：MySQL实验性项目</li>
</ul>
<h3 id="2-Downloads：MySQL下载"><a href="#2-Downloads：MySQL下载" class="headerlink" title="2. Downloads：MySQL下载"></a>2. <strong>Downloads</strong>：MySQL下载</h3><ul>
<li>Enterprise：MySQL企业版本相关，略过</li>
<li>Community：社区版，我们下载和使用社区版<ul>
<li>MySQL Community Server：MySQL Server</li>
<li>MySQL Fabric : 和管理相关的工具</li>
<li>MySQL Router：路由中间件</li>
<li>MySQL Utilities：MySQL应用程序包</li>
<li>MySQL Workbench：官方图型化管理界面</li>
<li>MySQL Proxy：MySQL代理。Alpha版本，不推荐</li>
</ul>
</li>
</ul>
<h3 id="3-Documentation：MySQL文档"><a href="#3-Documentation：MySQL文档" class="headerlink" title="3. Documentation：MySQL文档"></a>3. <strong>Documentation</strong>：MySQL文档</h3><ul>
<li>官方文档 版面更改，下载离线文档在左侧Menu的下面<ul>
<li><a href="http://downloads.mysql.com/docs/refman-5.7-en.a4.pdf" target="_blank" rel="noopener">PDF A4</a></li>
<li><a href="http://downloads.mysql.com/docs/refman-5.7-en.epub" target="_blank" rel="noopener">EPUB</a></li>
<li><a href="http://downloads.mysql.com/docs/refman-5.7-en.html-chapter.zip" target="_blank" rel="noopener">HTML</a></li>
</ul>
</li>
</ul>
<hr>
<h2 id="三-MySQL下载"><a href="#三-MySQL下载" class="headerlink" title="三. MySQL下载"></a>三. MySQL下载</h2><ol>
<li>推荐下载<code>Linux-Generic</code>版本</li>
<li><code>Source Code</code>版本主要作用是为了让开发人员研究源码使用，自己编译对性能提升不明显</li>
<li>不推荐<code>Version 5.5.X</code>，有部分bug</li>
<li>推荐使用<code>Version 5.6.X</code>和<code>Version 5.7.X</code></li>
<li><p>下载地址：</p>
<ul>
<li><p><a href="https://downloads.mysql.com/archives/get/file/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz" target="_blank" rel="noopener">MySQL Community Server 5.7.18 Linux Generic x86-64bit</a></p>
</li>
<li><p><a href="http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.27-linux-glibc2.5-x86_64.tar.gz" target="_blank" rel="noopener">MySQL Community Server 5.6.27 Linux Generic x86-64bit</a></p>
</li>
</ul>
</li>
</ol>
<hr>
<h2 id="四-MySQL安装"><a href="#四-MySQL安装" class="headerlink" title="四. MySQL安装"></a>四. MySQL安装</h2><h3 id="1-安装通用步骤："><a href="#1-安装通用步骤：" class="headerlink" title="1. 安装通用步骤："></a>1. 安装通用步骤：</h3><ul>
<li>解压缩<code>mysql-VERSION-linux-glibc2.5-x86_64.tar.gz</code></li>
<li>打开<code>INSTALL_BINARY</code> 文件，按照<code>shell&gt;</code>开头的步骤进行操作</li>
<li>将<code>export PATH=/安装路径/mysql/bin:$PATH</code>添加到<code>/etc/profile</code></li>
<li><code>chkconfig mysqld on</code>或者<code>chkconfig mysqld.server on</code>因你的环境而定。</li>
</ul>
<h3 id="2-MySQL-5-6-X-安装："><a href="#2-MySQL-5-6-X-安装：" class="headerlink" title="2. MySQL 5.6.X 安装："></a>2. MySQL 5.6.X 安装：</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">shell&gt; yum install libaio <span class="comment"># Debain系用户:apt-get install libaio1</span></span><br><span class="line">shell&gt; groupadd mysql</span><br><span class="line">shell&gt; useradd -r -g mysql mysql</span><br><span class="line">shell&gt; <span class="built_in">cd</span> /usr/<span class="built_in">local</span></span><br><span class="line">shell&gt; tar zxvf /path/to/mysql-VERSION-OS.tar.gz</span><br><span class="line">shell&gt; ln -s full-path-to-mysql-VERSION-OS mysql</span><br><span class="line">shell&gt; <span class="built_in">cd</span> mysql</span><br><span class="line">shell&gt; chown -R mysql .</span><br><span class="line">shell&gt; chgrp -R mysql .</span><br><span class="line">shell&gt; scripts/mysql_install_db --user=mysql</span><br><span class="line">shell&gt; chown -R root .</span><br><span class="line">shell&gt; chown -R mysql data</span><br><span class="line">shell&gt; bin/mysqld_safe --user=mysql &amp;</span><br><span class="line"><span class="comment"># Next command is optional</span></span><br><span class="line">shell&gt; cp support-files/mysql.server /etc/init.d/mysql.server</span><br></pre></td></tr></table></figure>
<h3 id="3-MySQL-5-7-X-安装"><a href="#3-MySQL-5-7-X-安装" class="headerlink" title="3. MySQL 5.7.X 安装"></a>3. MySQL 5.7.X 安装</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">shell&gt; groupadd mysql</span><br><span class="line">shell&gt; useradd -r -g mysql mysql</span><br><span class="line">shell&gt; <span class="built_in">cd</span> /usr/<span class="built_in">local</span></span><br><span class="line">shell&gt; tar zxvf /path/to/mysql-VERSION-OS.tar.gz</span><br><span class="line">shell&gt; ln -s full-path-to-mysql-VERSION-OS mysql</span><br><span class="line">shell&gt; <span class="built_in">cd</span> mysql</span><br><span class="line">shell&gt; mkdir mysql-files</span><br><span class="line">shell&gt; chmod 770 mysql-files</span><br><span class="line">shell&gt; chown -R mysql .</span><br><span class="line">shell&gt; chgrp -R mysql .</span><br><span class="line">shell&gt; bin/mysqld --initialize --user=mysql <span class="comment">#该步骤中会产生临时root@localhost密码</span></span><br><span class="line">shell&gt; bin/mysql_ssl_rsa_setup          </span><br><span class="line">shell&gt; chown -R root .</span><br><span class="line">shell&gt; chown -R mysql data mysql-files</span><br><span class="line">shell&gt; bin/mysqld_safe --user=mysql &amp;</span><br><span class="line"><span class="comment"># Next command is optional</span></span><br><span class="line">shell&gt; cp support-files/mysql.server /etc/init.d/mysql.server</span><br></pre></td></tr></table></figure>
<h3 id="4-验证安装"><a href="#4-验证安装" class="headerlink" title="4. 验证安装"></a>4. 验证安装</h3><ul>
<li><code>data</code>目录在安装之前是<code>空目录</code>，安装完成后应该有<code>ibXXX</code>等文件</li>
<li>安装过程中输出的信息中，不应该含有<code>ERROR</code>信息，错误信息<code>默认</code>会写入到<code>$HOSTNAME.err</code>的文件中</li>
<li>通过<code>bin/mysql</code>命令（<em>5.7.X含有临时密码</em>）可以正常登录</li>
</ul>
<h3 id="5-MySQL启动"><a href="#5-MySQL启动" class="headerlink" title="5. MySQL启动"></a>5. MySQL启动</h3><ul>
<li><code>mysqld_safe --user=mysql &amp;</code> 即可启动，<code>mysqld_safe</code>是一个守护<code>mysqld</code>进程的脚本程序，旨在<code>mysqld</code>意外停止时，可以重启<code>mysqld</code>进程</li>
<li>也可以通过<code>INSTALL_BINARRY</code>中的的步骤，使用<code>/etc/init.d/mysql.server  start</code>进行启动（启动脚本以你复制的实际名字为准，通常改名为<code>mysqld</code>,即<code>/etc/init.d/mysqld start</code>）</li>
</ul>
<hr>
<h2 id="五-附录"><a href="#五-附录" class="headerlink" title="五. 附录"></a>五. 附录</h2><h3 id="1-配置文件my-cnf"><a href="#1-配置文件my-cnf" class="headerlink" title="1. 配置文件my.cnf"></a>1. 配置文件<code>my.cnf</code></h3><figure class="highlight clean"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br></pre></td><td class="code"><pre><span class="line"># line:		    V1<span class="number">.0</span></span><br><span class="line"># mail:         gczheng@<span class="number">139.</span>com</span><br><span class="line"># date:         <span class="number">2017</span><span class="number">-10</span><span class="number">-01</span></span><br><span class="line"># file_name:    my.cnf</span><br><span class="line"># function:      </span><br><span class="line">## 注意：建议参数可需要根据实际情况作调整</span><br><span class="line">## 本配置文件主要适用于MySQL <span class="number">5.7</span><span class="number">.18</span>版本</span><br><span class="line"></span><br><span class="line"># ********* 以下重要参数必须修改核对 *********</span><br><span class="line"># <span class="number">1.</span>innodb_flush_log_at_trx_commit=<span class="number">1</span></span><br><span class="line"># <span class="number">2.</span>sync_binlog = <span class="number">1</span></span><br><span class="line"># <span class="number">3.</span>innodb_strict_mode = OFF                          #关闭InnoDB严格检查模式</span><br><span class="line"># <span class="number">4.</span>innodb_flush_method = O_DIRECT</span><br><span class="line"># <span class="number">5.</span>lower_case_table_names = <span class="number">0</span>                        #设置区分大小写</span><br><span class="line"># <span class="number">6.</span>character-set-server = utf8mb4</span><br><span class="line"># <span class="number">7.</span>sql_mode                                          #配置为空值</span><br><span class="line"># <span class="number">8.</span>server-id =<span class="number">1</span>                                      #修改成对应数值</span><br><span class="line"># <span class="number">9.</span>innodb_buffer_pool_size = <span class="number">10</span>G                     #纯mysql server 配置<span class="number">50</span>%和 混合内存配置不低于<span class="number">10</span>G~<span class="number">40</span>%</span><br><span class="line">#<span class="number">10.</span>key_buffer_size=<span class="number">1</span>G                                #如果有myisam表请配置为<span class="number">1</span>G，没有请配置<span class="number">64</span>M</span><br><span class="line">#<span class="number">11.</span>innodb_data_file_path = ibdata1:<span class="number">1</span>G:autoextend     #确认配置是否跟原来一样,之前已配置好请维持原样,如未配置请注释掉，新版本请取消注释</span><br><span class="line">#<span class="number">12.</span>log_bin = /r2/mysqldata/binlog                    #旧版本或者之前已配置好如：log_bin =/r2/mysqldata/slave-bin，请维持原样</span><br><span class="line">#<span class="number">13.</span>slave-parallel                                    #从库开启并行复制，并行复制参数取消注释</span><br><span class="line">#<span class="number">14.</span>undolog                                           #确认配置是否跟原来一样,之前已配置好请维持原样,如未配置请注释掉，新版本（包括升级版本）请取消注释并创建目录并授权</span><br><span class="line"># ********************************************</span><br><span class="line"></span><br><span class="line">[client]</span><br><span class="line">port    = <span class="number">3306</span></span><br><span class="line">socket  = /r2/mysqldata/mysql.sock</span><br><span class="line">#=======================================================================</span><br><span class="line"># # MySQL客户端配置</span><br><span class="line">#=======================================================================</span><br><span class="line">[mysql]</span><br><span class="line">prompt=<span class="string">"(\u@\h) \\R:\\m:\\s [\d]&gt; "</span></span><br><span class="line">no-auto-rehash</span><br><span class="line">default-character-set = utf8mb4</span><br><span class="line">#=======================================================================</span><br><span class="line"># MySQL服务器全局配置</span><br><span class="line">#=======================================================================</span><br><span class="line">[mysqld]</span><br><span class="line">user = mysql</span><br><span class="line">port = <span class="number">3306</span></span><br><span class="line">server-id = <span class="number">1</span></span><br><span class="line">tmpdir = /r2/mysqldata</span><br><span class="line">datadir = /r2/mysqldata</span><br><span class="line">socket  = /r2/mysqldata/mysql.sock</span><br><span class="line">wait_timeout = <span class="number">31536000</span></span><br><span class="line">#interactive_timeout = <span class="number">600</span></span><br><span class="line">sql_mode =                             #sql_mode 配置为空值</span><br><span class="line">#skip_name_resolve = <span class="number">1</span></span><br><span class="line">lower_case_table_names = <span class="number">0</span></span><br><span class="line">character-set-server = utf8mb4</span><br><span class="line">#skip-character-set-client-handshake   #忽略客户端的字符集，使用服务器的设置</span><br><span class="line">log_timestamps = SYSTEM</span><br><span class="line">init_connect= <span class="string">'SET NAMES utf8mb4'</span></span><br><span class="line">max_allowed_packet = <span class="number">128</span>M</span><br><span class="line">######################### 性能参数 ####################</span><br><span class="line">open_files_limit = <span class="number">10240</span></span><br><span class="line">max_connections = <span class="number">10000</span></span><br><span class="line">max_user_connections=<span class="number">9990</span></span><br><span class="line">max_connect_errors = <span class="number">100000</span></span><br><span class="line">table_open_cache = <span class="number">1024</span></span><br><span class="line">thread_cache_size = <span class="number">64</span></span><br><span class="line">max_heap_table_size = <span class="number">32</span>M</span><br><span class="line">query_cache_type = <span class="number">0</span></span><br><span class="line">###global cache ###</span><br><span class="line">key_buffer_size = <span class="number">1</span>G</span><br><span class="line">query_cache_size = <span class="number">0</span></span><br><span class="line">tmp_table_size = <span class="number">32</span>M        #内存临时表</span><br><span class="line">binlog_cache_size = <span class="number">4</span>M      #二进制日志缓冲</span><br><span class="line">###session cache ###</span><br><span class="line">sort_buffer_size = <span class="number">8</span>M       #排序缓冲</span><br><span class="line">join_buffer_size = <span class="number">4</span>M       #表连接缓冲</span><br><span class="line">read_buffer_size = <span class="number">8</span>M       #顺序读缓冲</span><br><span class="line">read_rnd_buffer_size = <span class="number">8</span>M   #随机读缓冲</span><br><span class="line">thread_stack = <span class="number">256</span>KB        #线程的堆栈的大小</span><br><span class="line">######################### binlog设置 #####################</span><br><span class="line">binlog_format = ROW</span><br><span class="line">log_bin = /r2/mysqldata/binlog</span><br><span class="line">max_binlog_size = <span class="number">1</span>G</span><br><span class="line">expire_logs_days = <span class="number">15</span>       #binlog比较占空间，注意磁盘空间</span><br><span class="line">sync_binlog = <span class="number">1</span>             #重要参数必须修改为<span class="number">1</span></span><br><span class="line">######################### 复制设置 ########################</span><br><span class="line">log_slave_updates = <span class="number">1</span></span><br><span class="line">#replicate-do-db = test</span><br><span class="line">#binlog-ignore-db = mysql</span><br><span class="line">### GTID 配置 ###</span><br><span class="line">gtid_mode=ON</span><br><span class="line">enforce-gtid-consistency=true</span><br><span class="line">#****************** 开启并行复制（从库）******************</span><br><span class="line">slave-parallel-type=LOGICAL_CLOCK     #基于组提交的并行复制方式</span><br><span class="line">slave-parallel-workers=<span class="number">8</span>              #并行的SQL线程数量</span><br><span class="line">master-info_repository=TABLE          #master信息以表的形式保存</span><br><span class="line">relay_log_info_repository=TABLE       #slave信息以表的形式保存</span><br><span class="line">relay_log_recovery=ON                 #relay_log自我修复</span><br><span class="line">######################### innodb ##########################</span><br><span class="line">default_storage_engine = InnoDB</span><br><span class="line">innodb_data_file_path = ibdata1:<span class="number">1</span>G:autoextend</span><br><span class="line">innodb_buffer_pool_size = <span class="number">10</span>G         #系统内存<span class="number">50</span>%</span><br><span class="line">innodb_open_files = <span class="number">5120</span></span><br><span class="line">innodb_flush_log_at_trx_commit = <span class="number">1</span>    #线上服务器必须配置为<span class="number">1</span></span><br><span class="line">innodb_file_per_table = <span class="number">1</span></span><br><span class="line">innodb_lock_wait_timeout = <span class="number">5</span></span><br><span class="line">innodb_io_capacity = <span class="number">400</span>              #根据您的服务器IOPS能力适当调整innodb_io_capacity，配SSD盘可调整到 <span class="number">10000</span> - <span class="number">20000</span></span><br><span class="line">innodb_io_capacity_max = <span class="number">20000</span></span><br><span class="line">innodb_flush_method = O_DIRECT</span><br><span class="line">innodb_log_file_size = <span class="number">2</span>G</span><br><span class="line">innodb_log_files_in_group = <span class="number">2</span></span><br><span class="line">innodb_large_prefix = <span class="number">0</span></span><br><span class="line">innodb_thread_concurrency = <span class="number">64</span></span><br><span class="line">innodb_strict_mode = OFF</span><br><span class="line">innodb_sort_buffer_size = <span class="number">4194304</span></span><br><span class="line">#****************** undolog设置 ******************</span><br><span class="line">#innodb_undo_directory = /r2/mysqldata/undolog    #undolog日志目录</span><br><span class="line">#innodb_undo_tablespaces = <span class="number">2</span>                      #undolog日志文件个数，mysql8之后将弃用</span><br><span class="line">#innodb_undo_logs = <span class="number">128</span>                           #回滚段的数量， 至少大于等于<span class="number">35</span>，默认<span class="number">128</span>。</span><br><span class="line">#innodb_max_undo_log_size = <span class="number">1</span>G                    #当超过这个阀值（默认是<span class="number">1</span>G），会触发truncate回收（收缩）动作，truncate后空间缩小到<span class="number">10</span>M。</span><br><span class="line">#innodb_purge_rseg_truncate_frequency = <span class="number">128</span>       #控制回收（收缩）undolog的频率</span><br><span class="line">#innodb_undo_log_truncate = <span class="number">1</span>                     #即开启在线回收undolog日志文件</span><br><span class="line">######################### log 设置 #####################</span><br><span class="line">log_error = /r2/mysqldata/error.log</span><br><span class="line">slow_query_log = <span class="number">1</span></span><br><span class="line">long_query_time = <span class="number">10</span></span><br><span class="line">slow_query_log_file = /r2/mysqldata/slow.log</span><br><span class="line">#=======================================================================</span><br><span class="line"># MySQL mysqldump配置</span><br><span class="line">#=======================================================================</span><br><span class="line">[mysqldump]</span><br><span class="line">quick</span><br><span class="line">max_allowed_packet = <span class="number">128</span>M</span><br><span class="line">#=======================================================================</span><br><span class="line"># MySQL mysqld_safe配置</span><br><span class="line">#=======================================================================</span><br><span class="line">[mysqld_safe]</span><br><span class="line">log_error = /r2/mysqldata/error.log</span><br><span class="line">pid_file = /r2/mysqldata/mysqldb.pid</span><br><span class="line"></span><br><span class="line">[mysqld<span class="number">-5.7</span>]</span><br><span class="line">innodb_buffer_pool_dump_pct = <span class="number">40</span></span><br><span class="line">innodb_page_cleaners = <span class="number">4</span></span><br><span class="line">innodb_undo_log_truncate = <span class="number">1</span></span><br><span class="line">innodb_max_undo_log_size = <span class="number">2</span>G</span><br><span class="line">innodb_purge_rseg_truncate_frequency = <span class="number">128</span></span><br><span class="line">binlog_gtid_simple_recovery=<span class="number">1</span></span><br><span class="line">log_timestamps=<span class="keyword">system</span></span><br><span class="line">transaction_write_set_extraction=MURMUR32</span><br><span class="line">show_compatibility_56=on</span><br></pre></td></tr></table></figure>
<h3 id="2-几个重要的参数配置和说明"><a href="#2-几个重要的参数配置和说明" class="headerlink" title="2. 几个重要的参数配置和说明"></a>2. 几个重要的参数配置和说明</h3><ul>
<li><code>innodb_log_file_size = 4G</code> :做实验可以更改的小点，线上环境推荐用4G，以前5.5和5.1等版本之所以官方给的值很小，是因为太大后有bug，现在bug已经修复</li>
<li><code>innodb_undo_logs = 128</code>和<code>innodb_undo_tablespaces = 3</code>建议在安装之前就确定好该值，后续修改比较麻烦</li>
<li><code>[mysqld]</code>，<code>[mysqld-5.7]</code>这种tag表明了下面的配置在什么版本下才生效,<code>[mysqld]</code>下均生效</li>
<li><code>datadir</code>, <code>innodb_log_group_home_dir</code>, <code>innodb_undo_directory</code>一定要注意他的权限是 <code>mysql:mysql</code></li>
</ul>
<h3 id="3-my-cnf问题"><a href="#3-my-cnf问题" class="headerlink" title="3. my.cnf问题"></a>3. <code>my.cnf</code>问题</h3><ul>
<li>使用<code>mysqld --help -vv | grep my.cnf</code>查看mysql的配置文件读取顺序</li>
<li>后读取的<code>my.cnf</code>中的配置，如果有相同项，会覆盖之前的配置</li>
<li>使用<code>--defaults-files</code>可指定配置文</li>
<li></li>
</ul>
<h3 id="4-安装脚本-run-02-install-new-mysql-version-sh"><a href="#4-安装脚本-run-02-install-new-mysql-version-sh" class="headerlink" title="4. 安装脚本 run_02_install_new_mysql_version.sh"></a>4. 安装脚本 <code>run_02_install_new_mysql_version.sh</code></h3><ul>
<li>注意：脚本、my.cnf文件和mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz放在同一目录下</li>
</ul>
<figure class="highlight perl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment"># line:           V1.0</span></span><br><span class="line"><span class="comment"># mail:           gczheng@139.com</span></span><br><span class="line"><span class="comment"># data:           2018-06-21</span></span><br><span class="line"><span class="comment"># script_name:    run_02_install_new_mysql_version.sh</span></span><br><span class="line"><span class="comment"># function:       Install mysql5.7.18</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#=======================================================================</span></span><br><span class="line"><span class="comment">#配置信息</span></span><br><span class="line"><span class="comment">#=======================================================================</span></span><br><span class="line">MYCNF=my.cnf</span><br><span class="line">MYSQL_SOURCE_PACKAGES=mysql-<span class="number">5.7</span>.<span class="number">18</span>-linux-glibc2.<span class="number">5</span>-x86_64.tar.gz</span><br><span class="line">MYSQL_DOWNLOAD_LINK=<span class="string">'http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz'</span></span><br><span class="line">MYSQL=<span class="regexp">/usr/local</span><span class="regexp">/mysql/bin</span><span class="regexp">/mysql</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">#=======================================================================</span></span><br><span class="line"><span class="regexp"># echo添加颜色</span></span><br><span class="line"><span class="regexp">#=======================================================================</span></span><br><span class="line"><span class="regexp">echo_color()&#123;</span></span><br><span class="line"><span class="regexp">    color=$&#123;1&#125; &amp;&amp; shift</span></span><br><span class="line"><span class="regexp">    case $&#123;color&#125; in</span></span><br><span class="line"><span class="regexp">        black)</span></span><br><span class="line"><span class="regexp">            echo -e "\e[0;30m$&#123;@&#125;\e[0m"</span></span><br><span class="line"><span class="regexp">            ;;</span></span><br><span class="line"><span class="regexp">        red)</span></span><br><span class="line"><span class="regexp">            echo -e "\e[0;31m$&#123;@&#125;\e[0m"</span></span><br><span class="line"><span class="regexp">            ;;</span></span><br><span class="line"><span class="regexp">        green)</span></span><br><span class="line"><span class="regexp">            echo -e "\e[0;32m$&#123;@&#125;\e[0m"</span></span><br><span class="line"><span class="regexp">            ;;</span></span><br><span class="line"><span class="regexp">        yellow)</span></span><br><span class="line"><span class="regexp">            echo -e "\e[0;33m$&#123;@&#125;\e[0m"</span></span><br><span class="line"><span class="regexp">            ;;</span></span><br><span class="line"><span class="regexp">        blue)</span></span><br><span class="line"><span class="regexp">            echo -e "\e[0;34m$&#123;@&#125;\e[0m"</span></span><br><span class="line"><span class="regexp">            ;;</span></span><br><span class="line"><span class="regexp">        purple)</span></span><br><span class="line"><span class="regexp">            echo -e "\e[0;35m$&#123;@&#125;\e[0m"</span></span><br><span class="line"><span class="regexp">            ;;</span></span><br><span class="line"><span class="regexp">        cyan)</span></span><br><span class="line"><span class="regexp">            echo -e "\e[0;36m$&#123;@&#125;\e[0m"</span></span><br><span class="line"><span class="regexp">            ;;</span></span><br><span class="line"><span class="regexp">        *)</span></span><br><span class="line"><span class="regexp">            echo -e "\e[0;37m$&#123;@&#125;\e[0m"</span></span><br><span class="line"><span class="regexp">            ;;</span></span><br><span class="line"><span class="regexp">    esac    # --- end of case ---</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">#=======================================================================</span></span><br><span class="line"><span class="regexp">#检查安装包、脚本、my.cnf是否齐全</span></span><br><span class="line"><span class="regexp">#=======================================================================</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">function chk_install_resource()</span></span><br><span class="line"><span class="regexp">&#123;</span></span><br><span class="line"><span class="regexp">	#判断 template_install-my.cnf 是否存在</span></span><br><span class="line"><span class="regexp">	if [ ! -f "$MYCNF" ];then</span></span><br><span class="line"><span class="regexp">		echo_color red "$(date +'%Y-%m-%d %H:%M:%S') $MYCNF file is not exits!$(echo_warning)"</span></span><br><span class="line"><span class="regexp">		exit 1</span></span><br><span class="line"><span class="regexp">	fi</span></span><br><span class="line"><span class="regexp">	#判断 MySQL Community Server 5.7.18 tar包是否存在</span></span><br><span class="line"><span class="regexp">	if [ ! -f "$MYSQL_SOURCE_PACKAGES" ];then</span></span><br><span class="line"><span class="regexp">		echo_color red "$(date +'%Y-%m-%d %H:%M:%S') $MYSQL_SOURCE_PACKAGES is not exits, please download it from $MYSQL_DOWNLOAD_LINK"</span></span><br><span class="line"><span class="regexp">		exit 1</span></span><br><span class="line"><span class="regexp">	fi</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br><span class="line"><span class="regexp">#=======================================================================</span></span><br><span class="line"><span class="regexp"># 添加帐号和目录</span></span><br><span class="line"><span class="regexp">#=======================================================================</span></span><br><span class="line"><span class="regexp">function create_sys_user()</span></span><br><span class="line"><span class="regexp">&#123;</span></span><br><span class="line"><span class="regexp">	#添加mysql用户信息</span></span><br><span class="line"><span class="regexp">	if id mysql &amp;&gt; /dev</span><span class="regexp">/null;then</span></span><br><span class="line"><span class="regexp">		echo_color red "$(date +'%Y-%m-%d %H:%M:%S') MySQL user is exits."</span></span><br><span class="line"><span class="regexp">	else</span></span><br><span class="line"><span class="regexp">		useradd -r -s /bin</span><span class="regexp">/false mysql &amp;&amp; echo_color cyan "$(date +'%Y-%m-%d %H:%M:%S') The system user is added to success .."</span></span><br><span class="line"><span class="regexp">	fi</span></span><br><span class="line"><span class="regexp">	#添加mysql数据目录/r</span>2/mysqldata和权限</span><br><span class="line">	<span class="keyword">if</span> [ -d <span class="string">"/r2"</span> ];then</span><br><span class="line">		<span class="keyword">if</span> [ ! -d <span class="string">"/r2/mysqldata"</span> ];then</span><br><span class="line">			<span class="keyword">mkdir</span> -p /r2/mysqldata &amp;&amp; echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') MySQL data directory is created .."</span></span><br><span class="line">			<span class="keyword">chown</span> -R mysql:mysql /r2/mysqldata</span><br><span class="line">			<span class="keyword">chmod</span> <span class="number">750</span> /r2/mysqldata</span><br><span class="line">		elif [ <span class="string">"$(ls -A /r2/mysqldata)"</span> = <span class="string">""</span> ];then</span><br><span class="line">			echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') MySQL data directory is exits."</span></span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">			echo_color red <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') MySQL data directory is not empty. Please check it."</span></span><br><span class="line">			<span class="keyword">exit</span> <span class="number">1</span></span><br><span class="line">		fi</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		echo_color red <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') /r2 directory is not exits. Please check the system config."</span></span><br><span class="line">		<span class="keyword">exit</span> <span class="number">1</span></span><br><span class="line">	fi</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">#=======================================================================</span></span><br><span class="line"><span class="comment">#检查是否有旧的mysql/mariadb版本存在</span></span><br><span class="line"><span class="comment">#=======================================================================</span></span><br><span class="line"></span><br><span class="line">function chk_old_mysql_version()</span><br><span class="line">&#123;</span><br><span class="line">	mysqlNum=$(rpm -qa | <span class="keyword">grep</span> -Ei <span class="string">'mysql|mariadb'</span>|wc -l)</span><br><span class="line">	<span class="keyword">if</span> [ <span class="string">"<span class="subst">$&#123;mysqlNum&#125;</span>"</span> -<span class="keyword">gt</span> <span class="string">"0"</span> ];then</span><br><span class="line">		echo_color red <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') The system has MySQL other version. There may be a conflict in the version！If it continues, the original database will be uninstall."</span></span><br><span class="line">		<span class="keyword">read</span> -p <span class="string">"Do you continue to install it(y/n):"</span> cn</span><br><span class="line">		case $cn in</span><br><span class="line">			<span class="keyword">y</span>|Y)</span><br><span class="line">				rpm -qa | <span class="keyword">grep</span> -Ei <span class="string">'mysql|mariadb'</span> | xargs yum remove -<span class="keyword">y</span> &amp;&gt; <span class="regexp">/dev/null</span></span><br><span class="line">				tar_install</span><br><span class="line">				;;</span><br><span class="line">			n|N)</span><br><span class="line">				<span class="keyword">exit</span> <span class="number">1</span></span><br><span class="line">				;;</span><br><span class="line">			*)</span><br><span class="line">				echo_color red <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Input ERROR."</span></span><br><span class="line">		esac</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') No old version was found."</span></span><br><span class="line">		tar_install</span><br><span class="line">	fi</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#=======================================================================</span></span><br><span class="line"><span class="comment"># 解压安装</span></span><br><span class="line"><span class="comment">#=======================================================================</span></span><br><span class="line"></span><br><span class="line">function tar_install()</span><br><span class="line">&#123;</span><br><span class="line">	echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Starting unzip $MYSQL_SOURCE_PACKAGES .."</span></span><br><span class="line">	tar zxvf $MYSQL_SOURCE_PACKAGES -C /usr/<span class="keyword">local</span>/ </span><br><span class="line">	echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Unzip $MYSQL_SOURCE_PACKAGES SUCCESS .."</span></span><br><span class="line">	<span class="keyword">if</span> [ ! -d <span class="string">"/usr/local/mysql"</span> ];then</span><br><span class="line">		ln -<span class="keyword">s</span> /usr/<span class="keyword">local</span>/<span class="string">`echo $MYSQL_SOURCE_PACKAGES |awk -F ".tar.gz" '&#123;printf $1&#125;'`</span>  /usr/<span class="keyword">local</span>/mysql</span><br><span class="line">		<span class="keyword">chown</span> -R mysql:mysql /usr/<span class="keyword">local</span>/mysql</span><br><span class="line">		<span class="keyword">chown</span> -R mysql:mysql /usr/<span class="keyword">local</span>/<span class="string">`echo $MYSQL_SOURCE_PACKAGES |awk -F ".tar.gz" '&#123;printf $1&#125;'`</span></span><br><span class="line">		<span class="keyword">chmod</span> <span class="number">750</span> /usr/<span class="keyword">local</span>/mysql</span><br><span class="line">		<span class="keyword">chmod</span> <span class="number">750</span> /usr/<span class="keyword">local</span>/<span class="string">`echo $MYSQL_SOURCE_PACKAGES |awk -F ".tar.gz" '&#123;printf $1&#125;'`</span></span><br><span class="line">		echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') MySQL package has been placed in the right position .."</span></span><br><span class="line">		cp -f $MYCNF /etc/my.cnf</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		<span class="keyword">read</span> -p <span class="string">"/usr/local/mysql install directory already exists, delete it, and continue(y/n):"</span> dn</span><br><span class="line">		case $dn in</span><br><span class="line">			<span class="keyword">y</span>|Y)</span><br><span class="line">				rm -rf /usr/<span class="keyword">local</span>/mysql</span><br><span class="line">				ln -<span class="keyword">s</span> /usr/<span class="keyword">local</span>/<span class="string">`echo $MYSQL_SOURCE_PACKAGES |awk -F ".tar.gz" '&#123;printf $1&#125;'`</span>  /usr/<span class="keyword">local</span>/mysql</span><br><span class="line">				<span class="keyword">chown</span> -R mysql:mysql /usr/<span class="keyword">local</span>/mysql</span><br><span class="line">				<span class="keyword">chown</span> -R mysql:mysql /usr/<span class="keyword">local</span>/<span class="string">`echo $MYSQL_SOURCE_PACKAGES |awk -F ".tar.gz" '&#123;printf $1&#125;'`</span></span><br><span class="line">				<span class="keyword">chmod</span> <span class="number">750</span> /usr/<span class="keyword">local</span>/mysql</span><br><span class="line">				<span class="keyword">chmod</span> <span class="number">750</span> /usr/<span class="keyword">local</span>/<span class="string">`echo $MYSQL_SOURCE_PACKAGES |awk -F ".tar.gz" '&#123;printf $1&#125;'`</span></span><br><span class="line">				echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') MySQL package has been placed in the right position .."</span></span><br><span class="line">				cp -f $MYCNF /etc/my.cnf</span><br><span class="line">				;;</span><br><span class="line">			n|N)</span><br><span class="line">				<span class="keyword">exit</span> <span class="number">1</span></span><br><span class="line">				;;</span><br><span class="line">			*)</span><br><span class="line">			echo_color red <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') /usr/local/mysql is exits.Please check it."</span></span><br><span class="line">		esac</span><br><span class="line">	fi</span><br><span class="line"></span><br><span class="line">	echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Starting initialization .."</span></span><br><span class="line">	/usr/<span class="keyword">local</span>/mysql/bin/mysqld --initialize --user=mysql  &amp;&gt; <span class="regexp">/dev/null</span> &amp;&amp; echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Initialization ......SUCCESS"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#=======================================================================</span></span><br><span class="line"><span class="comment"># 修改环境变量</span></span><br><span class="line"><span class="comment">#=======================================================================</span></span><br><span class="line">function add_system_profile()</span><br><span class="line">&#123;</span><br><span class="line">cat &gt;&gt; <span class="regexp">/etc/profile</span> &lt;&lt;EOF</span><br><span class="line">export PATH=\$PATH:<span class="regexp">/usr/local</span><span class="regexp">/mysql/bin</span><span class="regexp">/</span></span><br><span class="line"><span class="regexp">EOF</span></span><br><span class="line"><span class="regexp">source /etc</span><span class="regexp">/profile</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">function modify_system_env()</span></span><br><span class="line"><span class="regexp">&#123;</span></span><br><span class="line"><span class="regexp">#egrep "/usr</span><span class="regexp">/local/mysql</span><span class="regexp">/bin/</span><span class="string">" /etc/profile  &amp;&gt; /dev/null</span></span><br><span class="line"><span class="string">PROFILES=`grep -i  "</span>/usr/<span class="keyword">local</span>/mysql/bin/<span class="string">" /etc/profile |wc -l`</span></span><br><span class="line"><span class="string">if [ $PROFILES -eq 0 ];then</span></span><br><span class="line"><span class="string">	add_system_profile</span></span><br><span class="line"><span class="string">	echo_color cyan "</span>$(date +<span class="string">'%Y-%m-%d %H:%M:%S'</span>) Flush profile done ..<span class="string">"</span></span><br><span class="line"><span class="string">else</span></span><br><span class="line"><span class="string">	EXISTS=`grep -i  "</span>/usr/<span class="keyword">local</span>/mysql/bin/<span class="string">" /etc/profile |grep -e "</span>^<span class="comment">#" |wc -l`</span></span><br><span class="line">	<span class="keyword">if</span> [ $EXISTS -<span class="keyword">gt</span> <span class="number">0</span> ];then</span><br><span class="line">		add_system_profile</span><br><span class="line">		echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Flush profile done .."</span></span><br><span class="line">	fi</span><br><span class="line">fi</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#=======================================================================</span></span><br><span class="line"><span class="comment">#创建MySQL服务</span></span><br><span class="line"><span class="comment">#=======================================================================</span></span><br><span class="line"></span><br><span class="line">function el7_create_mysql_service()</span><br><span class="line">&#123;</span><br><span class="line">	cat &gt; <span class="regexp">/usr/lib</span><span class="regexp">/systemd/system</span><span class="regexp">/mysql.service &lt;&lt;EOF</span></span><br><span class="line"><span class="regexp">[Unit]</span></span><br><span class="line"><span class="regexp">Description=mysql</span></span><br><span class="line"><span class="regexp">After=syslog.target network.target remote-fs.target nss-lookup.target</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">[Service]</span></span><br><span class="line"><span class="regexp">Type=forking</span></span><br><span class="line"><span class="regexp">ExecStart=/usr</span><span class="regexp">/local/mysql</span><span class="regexp">/support-files/mysql</span>.server start</span><br><span class="line">ExecReload=<span class="regexp">/usr/local</span><span class="regexp">/mysql/support</span>-files/mysql.server restart</span><br><span class="line">ExecStop=<span class="regexp">/usr/local</span><span class="regexp">/mysql/support</span>-files/mysql.server stop</span><br><span class="line">LimitNOFILE = <span class="number">65535</span></span><br><span class="line">PrivateTmp=false</span><br><span class="line"></span><br><span class="line">[Install]</span><br><span class="line">WantedBy=multi-user.target</span><br><span class="line">EOF</span><br><span class="line"></span><br><span class="line">	systemctl daemon-reload &amp;&amp; echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Reload systemd services .."</span></span><br><span class="line">	systemctl enable mysql.service &amp;&amp; echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Enable MySQL systemd service .."</span></span><br><span class="line">	systemctl start mysql.service &amp;&amp; echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Starting MySQL......SUCCESS!"</span> || echo_color red <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Starting MySQL......FAILED!."</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function el6_create_mysql_service()</span><br><span class="line">&#123;</span><br><span class="line">	cd /usr/<span class="keyword">local</span>/mysql/support-files/</span><br><span class="line">	cp mysql.server /etc/init.d/mysql  </span><br><span class="line">	<span class="keyword">chmod</span> +<span class="keyword">x</span> /etc/init.d/mysql</span><br><span class="line">	chkconfig --add mysql   &amp;&amp; echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Add MySQL service for management .."</span></span><br><span class="line">	chkconfig --list mysql  &amp;&amp; echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') List MySQL service .."</span></span><br><span class="line">	/etc/init.d/mysql start &amp;&amp; echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Starting MySQL......SUCCESS!"</span> || echo_color red <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Starting MySQL......FAILED!."</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#=======================================================================</span></span><br><span class="line"><span class="comment"># 添加帐号</span></span><br><span class="line"><span class="comment">#=======================================================================</span></span><br><span class="line"></span><br><span class="line">function modify_mysql_account()</span><br><span class="line">&#123;</span><br><span class="line">	password=$(awk <span class="string">'/A temporary password/ &#123;print $NF&#125;'</span> /r2/mysqldata/error.log)</span><br><span class="line">	<span class="comment">#echo_color cyan "mysql temp password is $&#123;password&#125;"</span></span><br><span class="line">	<span class="keyword">if</span> [ <span class="string">"<span class="subst">$&#123;password&#125;</span>"</span> != <span class="string">""</span> ];then</span><br><span class="line">		$&#123;MYSQL&#125; -uroot -p<span class="string">"<span class="subst">$&#123;password&#125;</span>"</span> --<span class="keyword">connect</span>-expired-password -e <span class="string">"alter user root@localhost identified by 'iforgot';flush privileges;"</span> &amp;&gt; <span class="regexp">/dev/null</span> &amp;&amp; echo_color cyan  <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') 系统随机密码修改成功."</span></span><br><span class="line">		p1=$?</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		echo_color red  <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') MySQL密码获取失败，请排查/清除数据目录重新安装."</span></span><br><span class="line">		<span class="keyword">exit</span> <span class="number">1</span></span><br><span class="line">	fi</span><br><span class="line">	$&#123;MYSQL&#125; -uroot -piforgot -e <span class="string">"grant all privileges on *.* to gcdb@'%' identified by 'iforgot';"</span> &amp;&gt; <span class="regexp">/dev/null</span> &amp;&amp; echo_color cyan  <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') 授予gcdb@'%'用户通过任意主机操作所有数据库的所有权限成功."</span></span><br><span class="line">	p2=$?</span><br><span class="line">	<span class="keyword">if</span> [[ <span class="string">"<span class="subst">$&#123;p1&#125;</span>"</span> == <span class="string">"0"</span> &amp;&amp; <span class="string">"<span class="subst">$&#123;p2&#125;</span>"</span> == <span class="string">"0"</span> ]];then</span><br><span class="line">		echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') 现在可以登录mysql数据库，root@localhost和gcdb@'%'用户的默认密码是\033[41;37m iforgot \033[0m."</span></span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		echo_color red <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') 授权失败,请手动执行授权操作."</span></span><br><span class="line">	fi</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">#=======================================================================</span></span><br><span class="line"><span class="comment"># 开始安装mysql</span></span><br><span class="line"><span class="comment">#=======================================================================</span></span><br><span class="line">function mysql_install()</span><br><span class="line">&#123;</span><br><span class="line">	version=$(uname -r |awk -F <span class="string">'.'</span> <span class="string">'&#123; print $(NF-1) &#125;'</span>)</span><br><span class="line">	<span class="keyword">if</span> [ <span class="string">"<span class="subst">$&#123;version&#125;</span>"</span> != <span class="string">"el7"</span> ];then</span><br><span class="line">		echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Start install mysql for el6."</span></span><br><span class="line">		chk_install_resource</span><br><span class="line">		create_sys_user</span><br><span class="line">		chk_old_mysql_version</span><br><span class="line">		modify_system_env</span><br><span class="line">		el6_create_mysql_service</span><br><span class="line">		modify_mysql_account</span><br><span class="line">	<span class="keyword">else</span> </span><br><span class="line">		echo_color cyan <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') Start install mysql for el7."</span></span><br><span class="line">		chk_install_resource</span><br><span class="line">		create_sys_user</span><br><span class="line">		chk_old_mysql_version</span><br><span class="line">		modify_system_env</span><br><span class="line">		el7_create_mysql_service</span><br><span class="line">		modify_mysql_account</span><br><span class="line">	fi</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">mysql_install</span><br><span class="line">echo_color blue <span class="string">"$(date +'%Y-%m-%d %H:%M:%S') \033[42;37m run_02_install_new_mysql_version.sh执行完成 \033[0m"</span></span><br></pre></td></tr></table></figure>
          
        
      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  


          </div>
          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">郑冠常</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">1</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              

              
            </nav>
          

          

          

          
          

          
          

          
            
          
          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-snowflake-o"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">郑冠常</span>

  

  
</div>




  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动 v3.7.1</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a class="theme-link" target="_blank" href="https://theme-next.org">NexT.Gemini</a> v6.4.2</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
	
    

    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.4.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.4.2"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.4.2"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.4.2"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.4.2"></script>



  



  










  





  

  

  

  

  
  

  

  

  

  

  

</body>
</html>
